﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
using System.Configuration;
using Skynet.BusinessEntities;
using Microsoft.Practices.Unity;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
using System.ServiceModel;
using Infrastructure.Aspect.ExceptionHandling;

namespace SkynetWebServices
{
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class SkynetLibrary : DataService<SkyNetLibraryEntities>
    {
        private IUnityContainer ContainerService;
        public SkynetLibrary(): base()
        {
            this.ContainerService = System.Web.HttpContext.Current.Application["ContainerService"] as IUnityContainer;            
        }
        public static void InitializeService(DataServiceConfiguration config)
        {
             
            config.UseVerboseErrors = true;
            config.SetEntitySetAccessRule("*", EntitySetRights.All);
            config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
            config.MaxExpandDepth = 1; // Max Expand Depth 
            config.MaxResultsPerCollection = 100;
            config.DataServiceBehavior.AcceptCountRequests = true; 


        }
        protected override SkyNetLibraryEntities CreateDataSource()
        {  
            var es = this.ContainerService.Resolve<SkyNetLibraryEntities>();
            return es;
        }
        protected override void HandleException(HandleExceptionArgs args)
        {
            ExceptionPolicy.HandleException(args.Exception, ExceptionPolicyNames.ServiceInterfacePolicy);

            if (!(args.Exception is DataServiceException))
                args.Exception = new DataServiceException(500, "500", args.Exception.Message, System.Globalization.CultureInfo.CurrentCulture.Name, args.Exception);

            base.HandleException(args);
        }
    }
}
